{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "3576439e-2bcb-4609-afdf-353e82dc70d7",
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "\n",
    "df = pd.read_excel('新闻.xlsx')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "7231c045-6ce6-4b34-a2ed-9a24f5f9b498",
   "metadata": {},
   "outputs": [],
   "source": [
    "import jieba\n",
    "words = []\n",
    "for i, row in df.iterrows():\n",
    "    words.append(' '.join(jieba.cut(row['标题'])))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "e12a9539-1d4b-4a5c-a483-97c925cdef1b",
   "metadata": {},
   "outputs": [],
   "source": [
    "from sklearn.feature_extraction.text import CountVectorizer  #将文本转化为数值\n",
    "test = ['金融 科技 厉害', '华能 信托 厉害']\n",
    "vect = CountVectorizer()\n",
    "X = vect.fit_transform(test)\n",
    "X = X.toarray()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "id": "f78e0d7a-c32a-4eb0-9dfb-4b934284c06e",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'金融': 4, '科技': 3, '厉害': 2, '华能': 1, '信托': 0}\n"
     ]
    }
   ],
   "source": [
    "words_bag = vect.vocabulary_\n",
    "print(words_bag)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "id": "fee420eb-bb09-4850-8ab9-827bf45051af",
   "metadata": {},
   "outputs": [],
   "source": [
    "from sklearn.feature_extraction.text import CountVectorizer\n",
    "vect = CountVectorizer()\n",
    "X = vect.fit_transform(words)\n",
    "X = X.toarray()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "id": "2e063984-ef3e-4af0-b384-79fdbf776dc9",
   "metadata": {},
   "outputs": [],
   "source": [
    "words_bag = vect.vocabulary_"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "id": "811d3d3a-3781-433e-a0ce-9ae802c18228",
   "metadata": {},
   "outputs": [],
   "source": [
    "vect = CountVectorizer()\n",
    "X_test = vect.fit_transform(words[0:2])\n",
    "X_test = X_test.toarray()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "id": "68ead4f9-2b6e-4a3d-bfcb-fec5e584206f",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'信托公司': 6, '2019': 0, '上半年': 2, '经营': 8, '业绩': 3, '概览': 7, '首单': 10, '信托': 5, '企业': 4, 'abs': 1, '获批': 9}\n"
     ]
    }
   ],
   "source": [
    "words_bag = vect.vocabulary_ \n",
    "print(words_bag)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "7dc6e84f-3203-4771-a0f2-4591162ab007",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>信托公司</th>\n",
       "      <th>2019</th>\n",
       "      <th>上半年</th>\n",
       "      <th>经营</th>\n",
       "      <th>业绩</th>\n",
       "      <th>概览</th>\n",
       "      <th>首单</th>\n",
       "      <th>信托</th>\n",
       "      <th>企业</th>\n",
       "      <th>abs</th>\n",
       "      <th>获批</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   信托公司  2019  上半年  经营  业绩  概览  首单  信托  企业  abs  获批\n",
       "0     1     0    1   1   0   0   1   1   1    0   0\n",
       "1     0     1    0   0   1   1   0   0   0    1   1"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import pandas as pd\n",
    "df = pd.DataFrame(X_test, columns=words_bag)\n",
    "df"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "5312baac-0e39-48de-a31c-a28d913a6fb7",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "70713bd9-0e4c-450f-a300-61eb9d2fc2f1",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
